//
//  QChangePhoneViewController.m
//  QMCXDriver
//
//  Created by Massimo on 2017/4/19.
//  Copyright © 2017年 Massimo. All rights reserved.
//

#import "QChangePhoneViewController.h"
#import "LoginInputView.h"
@interface QChangePhoneViewController ()

{
    dispatch_source_t _timer;
}
@property (weak, nonatomic) IBOutlet LoginInputView *originPhoneInputView;
@property (weak, nonatomic) IBOutlet LoginInputView *changedPhoneInputView;
@property (weak, nonatomic) IBOutlet LoginInputView *passwordInputView;
@property (weak, nonatomic) IBOutlet LoginInputView *codeInputView;
@property (weak, nonatomic) IBOutlet UILabel *countDownLabel;

@property (copy, nonatomic) NSString *originNum;
@property (copy, nonatomic) NSString *changedNum;
@property (copy, nonatomic) NSString *password;
@property (copy, nonatomic) NSString *code;
@property (assign, nonatomic) NSUInteger countdownNum;
@end

@implementation QChangePhoneViewController


- (void)dealloc{
    NSLog(@"%s",__func__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"更改手机号";
    __weak typeof(self) weakSelf = self;
    [_codeInputView setAction:^(id object) {
        [weakSelf getCode];
    }];
    self.countDownLabel.hidden = YES;
    [RACObserve(self, countdownNum) subscribeNext:^(id x) {
        dispatch_async(dispatch_get_main_queue(), ^{
            weakSelf.countDownLabel.text = [NSString stringWithFormat:@"%02ld秒以后重新发送",[x unsignedIntegerValue]];
            if ([x unsignedIntegerValue] == 0) {
                [weakSelf stopCountdown];
            }
        });
        
    }];
    [self getSource];
}


- (void)getSource{
    
    __weak typeof(self) weakSelf = self;
    [self.originPhoneInputView setTextChange:^(NSString *text) {
        weakSelf.originNum = text;
    }];
    [self.changedPhoneInputView setTextChange:^(NSString *text) {
        weakSelf.changedNum = text;
    }];
    [self.passwordInputView setTextChange:^(NSString *text) {
        weakSelf.password = text;
    }];
    [self.codeInputView setTextChange:^(NSString *text) {
        weakSelf.code = text;
    }];
}


- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self stopCountdown];
}

//获取验证码
- (void)getCode{
    
    if(![QTools isValidateMobile:self.changedNum] ||
       self.changedNum.length < 11){
        [ProgressHUD showError:@"新手机号码输入有误"];
        return;
    }
    if( [self.changedNum isEqualToString:self.originNum]){
        [ProgressHUD showError:@"新手机号码不能与旧手机号码相同"];
        return;
    }
    
    
    
    [ProgressHUD show:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [ProgressHUD dismiss];
        [self startCountdown];
    });
}
//开始倒计时
- (void)startCountdown{
    __weak typeof(self) weakSelf = self;
    [[self.codeInputView valueForKey:@"actionButton"] setEnabled:NO];
#if DEBUG
   self.countdownNum = 3;
#else
    self.countdownNum = 60;
#endif
    
    self.countDownLabel.hidden = NO;
    
    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
    dispatch_source_set_timer(_timer, time , 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(_timer, ^{
        weakSelf.countdownNum -= 1;
    });
    dispatch_resume(_timer);
}

//停止倒计时
- (void)stopCountdown{
    [[self.codeInputView valueForKey:@"actionButton"] setEnabled:YES];
    if (_timer) {
        self.countDownLabel.hidden = YES;
        dispatch_cancel(_timer);
        _timer = nil;
    }
}


- (IBAction)save:(id)sender{
    [self stopCountdown];
    
    if (self.password.length < 6    ||
        self.originNum.length < 11  ||
        self.changedNum.length < 11 ||
        ![QTools isValidateMobile:self.originNum] ||
        ![QTools isValidateMobile:self.changedNum]) {
        [ProgressHUD showError:@"输入信息有误"];
        return;
    }
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
